package com.diorsunion.hedge.client.ui;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

/**
 * 文本面板
 * Created by harley-dog on 2016/5/23.
 */
public class TextPanel extends JPanel {
    private JTextArea m_textArea = new JTextArea();
    static final Font textComponentFont = new JList().getFont();
    static final String lineSeparator = System.getProperty("line.separator");
    private JScrollPane m_scrollPane = new JScrollPane(m_textArea);

    TextPanel(String title, boolean editable,Color backgroundColor,Color foregroundColor,Color textCaretColor) {
        super(new BorderLayout());
        if (title != null) {
            Border border = BorderFactory.createTitledBorder(title);
            setBorder(border);
        }
        m_textArea.setFont(textComponentFont);
        m_textArea.setBackground(backgroundColor);
        m_textArea.setForeground(foregroundColor);
        m_textArea.setCaretColor(textCaretColor);
        m_textArea.setEditable(editable);
        add(m_scrollPane);
    }

    void add(String line) {
        m_textArea.append(line + lineSeparator);
        moveCursorToEnd();
    }

    void setText(String text) {
        m_textArea.setText(text);
        if (m_textArea.isEditable()) {
            moveCursorToBeginning();
        } else {
            moveCursorToEnd();
        }
    }

    void moveCursorToBeginning() {
        m_textArea.setCaretPosition(0);
    }

    void moveCursorToEnd() {
        m_textArea.setCaretPosition(m_textArea.getText().length());
    }
}
